﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace QLSV
{
    public partial class FORMLOGIN : Form
    {
        public FORMLOGIN()
        {
            InitializeComponent();
        }

        private void FORMLOGIN_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string strDbPath = "QLSV.mdb";
            string strCnn = "Provider = Microsoft.Jet.OleDb.4.0; Data Source = " + strDbPath;
            OleDbConnection cnn = new OleDbConnection(strCnn);
            try
            {
                cnn.Open();

                string strSql = "SELECT COUNT(*) FROM USERTABLE WHERE USER = @ID AND PASS = @Password";
                OleDbCommand cmd = new OleDbCommand(strSql, cnn);
                cmd.Parameters.AddWithValue("@ID", txtUser.Text);
                cmd.Parameters.AddWithValue("@Password", txtPass.Text);
                if (Convert.ToBoolean(cmd.ExecuteScalar()))
                {
                    MessageBox.Show("Đăng nhập thành công!");

                    MAINFORM f = new MAINFORM();
                    
                    f.ShowDialog();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Đăng nhập thất bại!");
                }
                cnn.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TXTPASS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }  
        }
    }
}
